package cn.schoolwow.util.module.check.jsonarray.flow.check;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import com.alibaba.fastjson.JSONArray;

import java.util.ArrayList;
import java.util.List;

public class CheckJSONArrayContainValueFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        String fieldName = (String) flowContext.getData("fieldName");
        List<Object> containValueList = (List<Object>) flowContext.getData("containValueList");
        JSONArray array = (JSONArray) flowContext.checkData("array");
        String message = (String) flowContext.checkData("message");

        if(null==fieldName||fieldName.isEmpty()){
            throw new IllegalArgumentException(message+"的包含字段名称不能为空");
        }
        if(null==containValueList||containValueList.isEmpty()){
            throw new IllegalArgumentException(message+"包含列表不能为空");
        }

        List fieldValueList = new ArrayList();
        for(int i=0;i<array.size();i++){
            Object fieldValue = array.getJSONObject(i).get(fieldName);
            if(null==fieldValue){
                throw new IllegalArgumentException(message+"的字段值为空!");
            }
            fieldValueList.add(fieldValue);
        }
        for(Object containValue:containValueList){
            if(!fieldValueList.contains(containValue)){
                throw new IllegalArgumentException(message+"未包含指定值!指定值:"+containValueList+"!");
            }
        }
    }

    @Override
    public String name() {
        return "实例列表-检查字段值是否包含指定列表值";
    }
}
